package com.qwz.example;

import java.util.List;
/**
* @Description: 适配器，将文件日志适配数据库日志操作
* @Package com.qwz.example.Adapter
* @author Qiuwuzhi
* @date 2019/12/17 14:25
* @version V1.0
*/
public class Adapter implements LogDBOperateApi {

    private LogFileOperateApi adaptee;

    public Adapter(LogFileOperateApi adaptee) {
        this.adaptee = adaptee;
    }

    public void createLog(LogModel model) {
        List<LogModel> logFile = adaptee.readLogFile();
        logFile.add(model);
        adaptee.writeLogFile(logFile);
    }

    public void updateLog(LogModel model) {
        List<LogModel> logFile = adaptee.readLogFile();
        for (int i=0;i<logFile.size();i++) {
            if (logFile.get(i).getLogId().equals(model.getLogId())){
                logFile.set(i,model);
                break;
            }
        }
        adaptee.writeLogFile(logFile);
    }

    public void removeLog(LogModel model) {
        List<LogModel> logFile = adaptee.readLogFile();
        logFile.remove(model);
        adaptee.writeLogFile(logFile);
    }

    public List<LogModel> getAllLog() {
        return adaptee.readLogFile();
    }
}
